// pages/youhuidafangsong/youhuidafangsong.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    diquxina: true,
    shiqu: [{
        shiji: '西安',
        bianhao: 61012777,
      },
      {
        shiji: '咸阳',
        bianhao: 61000077,
      },
      {
        shiji: '汉中',
        bianhao: 61000014,
      },
      {
        shiji: '安康',
        bianhao: 61000103,
      },
      {
        shiji: '延安',
        bianhao: 61000040,
      },
      {
        shiji: '渭南',
        bianhao: 61000091,
      },
      {
        shiji: '铜川',
        bianhao: 61000065,
      },
      {
        shiji: '宝鸡',
        bianhao: 61000127,
      },
      {
        shiji: '榆林',
        bianhao: 61000038,
      },
      {
        shiji: '商洛',
        bianhao: 61000026,
      },
    ],
    USER_ASSETS_IDCARD: '', //手机号
    USER_ASSETS_NAME: '',
    ressj: '',
    timer: "", // 定时器名字
    countDownNum: '60',
    verify: "",
    L2code: '',
    bianhao: '',
    quyu: '',
  },




  back() {
    wx.navigateBack({
      delta: 1
    })
  },
  quyuxinxi(e) {
    console.log(e.currentTarget.dataset.item.shiji);
    this.setData({
      diquxina: true,
      quyu: e.currentTarget.dataset.item.shiji,
      bianhao: e.currentTarget.dataset.item.bianhao,

    })

  },

  xuandi() {
    this.setData({
      diquxina: false,


    })

  },


  //登录接口




  login_a: function () {

    if (this.data.quyu == '' || this.data.USER_ASSETS_IDCARD == '' || this.data.verify == '') {
      wx.showToast({
        title: '请完善信息！',
        icon: 'none',
        duration: 1500
      })
    } else {



      wx.request({
        url: app.globalData.interface+'/squareDance/API_5321_postBigBroadcast',
        method: 'POST',
        data: {
          USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD,
          code: this.data.verify,

          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
          L2code: this.data.bianhao,

        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          console.log(res.data);
          this.setData({
            ressj: res.data
          })

          if (res.data.result == 'error') {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else { //往领券跳转时跳转带数据
            wx.navigateTo({
              // url: '../../pages/weihuyancaolingqu/weihuyancaolingqu?data=' + JSON.stringify(this.data.ressj)
              url: '../../pages/youhuidafangsongqunye/youhuidafangsongqunye?data=' + JSON.stringify(this.data.ressj)

            })
          }
        }
      })

    }
  },

  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },


  fetch: function () {
    if (this.data.USER_ASSETS_IDCARD == '') {
      wx.showToast({
        title: '请填写手机号！',
        icon: 'none',
        duration: 3000
      })
    } else {
      wx.request({
        url: app.globalData.interface+'/app/API_1052_sendMassage',
        method: 'GET',
        data: {
          phone: this.data.USER_ASSETS_IDCARD,
          KEY: 'xianjgjKey9999',
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {

          if (res.data.result == 'error') {
            wx.showToast({
              title: res.data.code,
              icon: 'none',
              duration: 3000
            })
          } else {
            wx.showToast({
              title: res.data.code,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
      var _this = this;
      var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
      var timer = setInterval(function () {
        countDownNum -= 1;
        _this.setData({
          countDownNum: countDownNum
        })
        if (countDownNum <= -1) {
          clearInterval(timer);
          // 取消置顶的setInterval函数将要执行的代码
          _this.setData({
            countDownNum: 60,
          })
        }
      }, 1000)

    }
  },






  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this

    if (that.data.jiami == '' || that.data.session_key == '') {

      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) { //wx.login获取code。
            console.log(res.code);
            //发起网络请求

            wx.request({
              url: app.globalData.interface+'/post/API_1062_appGetOpenid',
              // url: 'http://192.168.3.173:8081/post/API_1062_appGetOpenid',
              method: 'GET',
              //向后端发送的数据
              data: {
                code: res.code //将code发送到后台服务器。
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
              },
              success: res => {
                console.log(res);
                if (res.data.result != 'success') {
                  console.log(1);
                  // wx.showToast({
                  //   title: '请获取手机号',
                  //   duration: 1000
                  // })
                } else {
                  var openid = res.data.openid;
                  app.globalData.openid = openid;

                  console.log(res.data.openid);
                  that.setData({
                    openid: res.data.openid
                  })
                  // that.scanCod()

                  that.setData({


                    jiami: app.globalData.jiami,
                    OPEN_ID: app.globalData.openid,
                    openid: app.globalData.openid,
                    session_key: app.globalData.session_key,
                  })
                  console.log(that.data.phone);
                  console.log(that.data.jiami);
                  console.log(that.data.openid);
                  console.log(that.data.session_key);
                }

              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    } else {

      console.log('成功');
    }



  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }

})